#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Configuration;
using System.Security;
using System.Diagnostics;
using AirWeb.Security;
using AirWeb.Security.FileSecurity;
using AirWeb.ApplicationTools.Common;
using AirWeb.Support.Diagnostics;

namespace FlashBack.Web
{
	/// <summary>
	/// Summary description for LogsViewer.
	/// </summary>
	public partial class LogsViewer : PageBase
	{

		public LogsViewer()
		{
			Page.Init += new System.EventHandler(Page_Init);
		}

		protected void Page_Load(object sender, System.EventArgs e)
		{
			Response.Expires = -1;

			if(!CurrentUser.IsAdministrator())
			{
				throw new SecurityException("You are not authorized to view this page");
			}
			
			if(!IsPostBack)
				BuildTable();
		}

		protected void BuildTable()
		{
			DataTable dt = new DataTable();
			
			DataRow dr;
			dt.Columns.Add(new DataColumn("Date", typeof(DateTime)));
			dt.Columns.Add(new DataColumn("User", typeof(string)));
			dt.Columns.Add(new DataColumn("Domain Address", typeof(string)));
			//dt.Columns.Add(new DataColumn("Host", typeof(string)));

			NameValueCollection config = (NameValueCollection)
				ConfigurationSettings.GetConfig("flashBack/directories");	

			string hostedfor = config["HOSTED_FOR"];

			EventLog log = ApplicationLog.Log;

			if(log == null)
			{
				ErrorMsg.Text = "Unable to open Flashback log file. This happens if the application does not have a permission to access the registry to create a log file";
				ErrorMsg.Visible = true;
				return;
			}

			foreach( EventLogEntry e in log.Entries)
			{
				Hashtable h;

				if(e.Source != "FlashBack") // Show only FlashBack entries
					continue;

				if(e.EntryType == EventLogEntryType.Information)
				{
					h = CommonHelper.ParseLogEntry(e.Message);
				}
				else
				{
					h = new Hashtable();
					h["Host"] = hostedfor;
					h["From"] = "";
					h["User"] = e.Message;
				}

				string s = (string)h["Host"];

				if(s != hostedfor)
					continue;

				dr = dt.NewRow();
				dr[0] = e.TimeGenerated;

				string u = (string)h["User"];
				if(u != null)
					dr[1] = u;
				
				string f = (string)h["From"];
				if(f != null)
					dr[2] = f;

				//if(s != null)
				//	dr[3] = s;
				
				dt.Rows.InsertAt(dr,0);
			}
            
			LogsGrid.DataSource = new DataView(dt);
			LogsGrid.DataBind();
		}

		protected void ChangePageIndex(object Sender, DataGridPageChangedEventArgs e)
		{
			LogsGrid.CurrentPageIndex = e.NewPageIndex;
			BuildTable();
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
		}

		public void ItemCommand(object sender, DataGridCommandEventArgs e)
		{
		}

		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
	}
}
